package com.atguigu.ssyx.activity.controller;


import com.atguigu.ssyx.activity.service.ActivityInfoService;
import com.atguigu.ssyx.model.activity.ActivityInfo;
import com.atguigu.ssyx.model.product.SkuInfo;
import com.atguigu.ssyx.vo.activity.ActivityRuleVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import com.atguigu.ssyx.commom.result.Result;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 活动表 前端控制器
 * </p>
 *
 * @author ZhangYK
 * @since 2024-07-09
 */
@RestController
@RequestMapping("/admin/activity/activityInfo")
@CrossOrigin
public class ActivityInfoController {

    @Resource
    private ActivityInfoService activityInfoService;
    //分页查询
    // url: `${api_name}/${page}/${limit}`,
    //      method: 'get'
    @ApiOperation(value = "获取分页列表")
    @GetMapping("{page}/{limit}")
    public Result list(@PathVariable Long page, @PathVariable Long limit) {
        Page<ActivityInfo> activityInfoPage = new Page<>(page, limit);
        IPage<ActivityInfo> activityInfoIPage = activityInfoService.selectPage(activityInfoPage);
        return Result.ok(activityInfoIPage);
    }

    //根据id查询
    // url: `${api_name}/get/${id}`,
    //      method: 'get'
    @GetMapping("get/{id}")
    public Result get(@PathVariable Long id) {
        ActivityInfo activityInfo = activityInfoService.getById(id);
        activityInfo.setActivityTypeString(activityInfo.getActivityType().getComment());
        return Result.ok(activityInfo);
    }

    //保存
    //url: `${api_name}/save`,
    //      method: 'post',
    //      data: role
    @PostMapping("save")
    public Result save(@RequestBody ActivityInfo activityInfo) {
        activityInfoService.save(activityInfo);
        return Result.ok(null);
    }

    //修改
    //url: `${api_name}/update`,
    //      method: 'put',
    //      data: role
    @PutMapping("update")
    public Result update(@RequestBody ActivityInfo activityInfo) {
        activityInfoService.updateById(activityInfo);
        return Result.ok(null);
    }

    // url: `${api_name}/remove/${id}`,
    //      method: 'delete'
    //根据id删除
    @DeleteMapping("remove/{id}")
    public Result remove(@PathVariable Long id) {
        activityInfoService.removeById(id);
        return Result.ok(null);
    }

    //批量删除
    // url: `${api_name}/batchRemove`,
    //      method: 'delete',
    //      data: idList
    @DeleteMapping("batchRemove")
    public Result batchRemove(@RequestBody List<Long> ids) {
        activityInfoService.removeByIds(ids);
        return Result.ok(null);
    }

    //营销活动规则接口

    //根据活动id获取规则数据
    //url: `${api_name}/findActivityRuleList/${id}`,
    //      method: 'get'

    /**
     * 根据活动id获取规则数据
     * @param id
     * @return
     */
    @GetMapping("findActivityRuleList/{id}")
    public Result findActivityRuleList(@PathVariable Long id) {
        Map<String,Object> activityRuleMap =
                activityInfoService.findActivityRuleList(id);
        return Result.ok(activityRuleMap);
    }


    //在活动里面添加规则
    // url: `${api_name}/saveActivityRule`,
    //      method: 'post',
    //      data: rule
    @PostMapping("saveActivityRule")
    public Result saveActivityRule(@RequestBody ActivityRuleVo activityRuleVo) {
        activityInfoService.saveActivityRule(activityRuleVo);
        return Result.ok(null);
    }

    //添加活动范围
    //url: `${api_name}/findSkuInfoByKeyword/${keyword}`,
    //      method: 'get'
    @GetMapping("findSkuInfoByKeyword/{keyword}")
    public Result findSkuInfoByKeyword(@PathVariable String keyword) {
        List<SkuInfo> list = activityInfoService.findSkuInfoByKeyword(keyword);
        return Result.ok(list);
    }

}

